﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using OnTheWay.Exceptions;

namespace OnTheWay.Command
{
    public class DelegateCommand: ICommand, INotifyPropertyChanged
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        private string _dispalyName;
        private bool _isChcked = false;
        private bool _isEnabled = true;
        private Visibility _visibility = Visibility.Visible;

        /// <summary>
        /// 是否显示
        /// </summary>
        public Visibility Visibility
        {
            get { return _visibility; }
            set
            {
                if (_visibility == value) return;
                _visibility = value;
                this.PropertyNotifyChanged("Visibility");
            }
        }

        /// <summary>
        /// 是否禁用
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                if (_isEnabled == value) return;
                _isEnabled = value;
                PropertyNotifyChanged("IsEnabled");
            }
        }

        public Type OwerType
        {
            get;
            set;
        }

        /// <summary>
        /// 命令名称
        /// </summary>
        public string DisplayName
        {
            get { return _dispalyName; }
            set
            {
                if (_dispalyName == value) return;
                _dispalyName = value;
                PropertyNotifyChanged("DisplayName");
            }
        }
        /// <summary>
        /// 命令选中否 默认不被选中
        /// </summary>
        public bool IsChecked
        {
            get { return _isChcked; }
            set
            {
                if (_isChcked == value) return;
                _isChcked = value;
                PropertyNotifyChanged("IsChecked");
            }
        }
        public string Name { get; set; }
        public string BuiltName { get; set; }

        public event EventHandler CanExecuteChanged;
        public bool HasCanExecuted
        {
            get { return _canExecute != null; }
        }

        public DelegateCommand(Action<object> execute, string name, string builtname)
            : this(execute, null, name, builtname)
        { }

        public DelegateCommand(Action<object> execute, Predicate<object> canExecute, string name, string builtname)
        {
            _execute = execute;
            _canExecute = canExecute;
            DisplayName = builtname;
            BuiltName = builtname;
            Name = name;
            OwerType = Type.SINGLE;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            try
            {
                _execute(parameter);
            }
            catch (System.Exception ex)
            {
                ExceptionHandle.Handle(ex, "DelegateCommand 执行命令出现异常");
            }
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        #region 实现接口

        public event PropertyChangedEventHandler PropertyChanged;
        protected void PropertyNotifyChanged(String propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        #endregion //实现接口

        #region 内部类

        public enum Type
        {
            SINGLE,
            MORE
        }

        #endregion 内部类
    }
}
